<?php
/**
 * Cache logic.
 *
 * @category Zend
 * @package Disputeo
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc.
 * @version $Id$
 * @link http://framework.zend.com
 * @since Available since release 1.0
 * @license GPL v3
 */

class Disputeo_Cache extends Zend_Cache
{
    /**
     * @var Disputeo_Config
     */
    protected static $_instance;
    
    /**
     * @var Array
     */
    protected $_frontendOptions;
    
    /**
     * @var Array 
     */
    protected $_backendOptions;
    
    /**
     * @var Zend_Config_Ini 
     */
    protected $_config;
    
    /**
     * @var Zend_Cache_Core 
     */
    protected $_coreCache;

    /**
     * Set default core values
     */
    public function __construct()
    {
        $this->_config = Disputeo_Config::config($this)->cache;
        $this->_frontendOptions = array(
            'lifetime' => $this->_config->default->lifetime,
            'automatic_serialization' => true
        );
        
        $this->_backendOptions['cache_dir'] = $this->_config->file->path;
    }
    
    /**
     * Get a class cache object
     * 
     * @param Object $class
     * @return Zend_Cache_Frontend_Class
     */
    public function getClassCache($class)
    {
        $this->_frontendOptions['cached_entity'] = $class;
        
        $classCache = $this::factory('Class', 'File', 
            $this->_frontendOptions, $this->_backendOptions);
        
        return $classCache;
    }
    
    /**
     * Get a default cache object
     * 
     * @return Zend_Cache_Core
     */
    public function getCache()
    {
        if ($this->_coreCache) {
            return $this->_coreCache;
        }
        
        $cache = $this::factory('Core', 'File',
            $this->_frontendOptions, $this->_backendOptions);
        $this->_coreCache = $cache;
        
        return $cache;
    }
    
    /**
     * Load cached data
     * 
     * @param type $id
     * @return mixed
     */
    public function load($id)
    {
        return $this->getCache()->load($id);
    }
    
    /**
     * Save data to cache
     * 
     * @param type $data
     * @param type $id
     * @return boolean
     */
    public function save($data, $id)
    {
        return $this->getCache()->save($data, $id);
    }
    
    /**
     * Zend class method cache factory
     * 
     * @param Object $class
     * @return Zend_Cache_Frontend_Class
     */
    public static function classCache($class)
    {
        $cache = self::cache();
        $classCache = $cache->getClassCache($class);
        
        return $classCache;
    }
    
    /**
     * Load data from default cache
     * 
     * @param type $cacheId
     * @return mixed 
     */
    public static function loadFromCache($cacheId)
    {
        $cache = self::cache();
        $coreCache = $cache->getCache();
        
        return $coreCache->load($cacheId);
    }
    
    /**
     * Save data to default cache
     * 
     * @param mixed $data
     * @param string $cacheId
     */
    public static function saveToCache($data, $cacheId)
    {
        $cache = self::cache();
        $coreCache = $cache->getCache();
        
        $coreCache->save($data, $cacheId);
    }
    
    /**
     * @return \self
     */
    public static function cache()
    {
        if (! self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}